package com.zjwd.web.controller.monitor;

import com.zjwd.common.annotation.Log;
import com.zjwd.common.core.controller.BaseController;
import com.zjwd.common.core.domain.AjaxResult;
import com.zjwd.common.core.page.TableDataInfo;
import com.zjwd.common.enums.BusinessType;
import com.zjwd.common.utils.poi.ExcelUtils;
import com.zjwd.framework.web.service.SysPasswordService;
import com.zjwd.system.domain.SysLogLogin;
import com.zjwd.system.service.ISysLogLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author liquan
 */
@RestController
@RequestMapping("/monitor/log/login")
public class SysLogLoginController extends BaseController {
    @Autowired
    private ISysLogLoginService logLoginService;

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:log:login:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLogLogin logLogin) {
        startPage();
        List<SysLogLogin> list = logLoginService.selectList(logLogin);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:log:login:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogLogin logLogin) {
        List<SysLogLogin> list = logLoginService.selectList(logLogin);
        ExcelUtils<SysLogLogin> utils = new ExcelUtils<SysLogLogin>(SysLogLogin.class);
        utils.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:log:login:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(logLoginService.deleteByIds(infoIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:log:login:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        logLoginService.clean();
        return success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:log:login:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userAccount}")
    public AjaxResult unlock(@PathVariable("userAccount") String userAccount) {
        passwordService.clearLoginRecordCache(userAccount);
        return success();
    }
}
